package com.maodou.android.ui.page.mine

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.foundation.ScrollState
import com.maodou.android.ui.page.DataStoreDemoScreen
import com.maodou.android.ui.page.DetailKey
import com.maodou.core.navigation.GlobalUiBus
import com.maodou.core.navigation.GlobalUiEvent
import com.maodou.core.navigation.NavigationAnimation
import com.maodou.core.navigation.Navigator3
import com.maodou.core.utils.permission.PermissionUtils

/**
 * @Author Xing
 * @CreateTime 2025年09月16日
 * @Description TODO
 */
@Composable
fun MineScreen(
    scrollState: ScrollState? = null // 供外部注入以保持滚动状态与回顶
) {
    val context = LocalContext.current
    var selected by remember { mutableStateOf(0) }

    val effectiveScrollState = scrollState ?: rememberSaveable(saver = ScrollState.Saver) { ScrollState(0) }

    Column(
        Modifier
            .fillMaxSize()
            .verticalScroll(effectiveScrollState)
            .padding(16.dp)
    ) {
        // 新增：跳转到带参页面的按钮（Nav3 键）- 展示不同动画效果
        Row(Modifier.fillMaxWidth()) {
            Button(onClick = {
                Navigator3.navigate(
                    DetailKey(
                        title = "Slide Animation",
                        id = 101
                    ),
                    NavigationAnimation.SLIDE
                )
            }) {
                Text("Slide Detail")
            }
            Spacer(Modifier.width(8.dp))
            Button(onClick = {
                Navigator3.navigate(
                    DetailKey(
                        title = "Scale Animation",
                        id = 102
                    ),
                    NavigationAnimation.SCALE
                )
            }) {
                Text("Scale Detail")
            }
        }
        Spacer(Modifier.height(8.dp))
        Row(Modifier.fillMaxWidth()) {
            Button(onClick = {
                Navigator3.navigate(
                    DetailKey(
                        title = "Fade Animation",
                        id = 103
                    ),
                    NavigationAnimation.FADE
                )
            }) {
                Text("Fade Detail")
            }
            Spacer(Modifier.width(8.dp))
            Button(onClick = {
                Navigator3.navigate(
                    DetailKey(
                        title = "No Animation",
                        id = 104
                    ),
                    NavigationAnimation.NONE
                )
            }) {
                Text("No Anim Detail")
            }
        }
        Spacer(Modifier.height(12.dp))
        Row {
            Button(onClick = { selected = 0 }) { Text("Network Demo") }
            Spacer(Modifier.weight(1f))
            Button(onClick = { selected = 1 }) { Text("DataStore Demo") }
            Button(onClick = { GlobalUiBus.events.tryEmit(GlobalUiEvent.ShowLoading("加载中...")) }) { Text("吐司测试") }
        }
        Spacer(Modifier.height(12.dp))
        Button(onClick = { PermissionUtils.requestCameraAndStoragePermissions(){} }) { Text("申请权限") }
        Spacer(Modifier.height(12.dp))
        when (selected) {
            0 -> {}
            else -> DataStoreDemoScreen(modifier = Modifier)
        }
        Spacer(Modifier.height(48.dp))
    }
}